<!doctype html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>

  <meta charset="utf-8">

  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>

  <link rel="import" href="../../polymer-micro.html">
  <link rel="import" href="micro-elements.html">

</head>
<body>

<x-trivial></x-trivial>

<x-trivial2></x-trivial2>

<input is="x-extension">

<script>

  suite('polymer-micro', function() {

    test('polymer-micro element created', function() {
      assert.equal(document.querySelector('x-trivial').textContent, 'x-trivial');
    });

    test('polymer-micro type extension element created', function() {
      assert.equal(document.querySelector('input').value, 'x-extension');
    });

  });

  suite('type checking & constructor', function() {

    test('Polymer.isInstance for non-instance', function() {
      var el = document.createElement('div');
      assert.isTrue(Polymer.instanceof(el, HTMLElement));
      assert.isTrue(Polymer.instanceof(el, HTMLDivElement));
      assert.isFalse(Polymer.isInstance(el));
    });

    test('document.createElement', function() {
      var MyElement = Polymer({is: 'my-basic'});
      var el = document.createElement('my-basic');
      assert.isTrue(Polymer.instanceof(el, HTMLElement));
      assert.isTrue(Polymer.instanceof(el, MyElement));
      assert.isTrue(Polymer.isInstance(el));
    });

    test('normal constructor', function() {
      var MyElement = Polymer({is: 'my-element'});
      var el = new MyElement();
      document.body.appendChild(el);
      if (Object.__proto__) {
        // instanceof Constructor only supported where proto swizzling is possible
        assert.instanceOf(el, MyElement, 'Instance of MyElement');
      }
      assert.instanceOf(el, HTMLElement, 'Instance of HTMLElement');
      assert.isTrue(Polymer.instanceof(el, HTMLElement));
      assert.isTrue(Polymer.instanceof(el, MyElement));
      assert.isTrue(Polymer.isInstance(el));
    });

    test('type-extension constructor', function() {
      var MyInput = Polymer({is: 'my-input', extends: 'input'});
      var el = new MyInput();
      document.body.appendChild(el);
      if (Object.__proto__) {
        // instanceof Constructor only supported where proto swizzling is possible
        assert.instanceOf(el, MyInput, 'Instance of MyInput');
        assert.instanceOf(el, HTMLElement, 'Instance of HTMLInputElement');
      }
      assert.isTrue(Polymer.instanceof(el, HTMLInputElement));
      assert.isTrue(Polymer.instanceof(el, HTMLElement));
      assert.isTrue(Polymer.instanceof(el, MyInput));
      assert.isTrue(Polymer.isInstance(el));
    });

    test('custom constructor', function() {
      var MyElement2 = Polymer({
        is: 'my-element2',
        factoryImpl: function(title){
          this.title = title;
        }
      });
      var el = new MyElement2('my title');
      document.body.appendChild(el);
      if (Object.__proto__) {
        // instanceof Constructor only supported where proto swizzling is possible
        assert.instanceOf(el, MyElement2, 'Instance of MyElement');
        assert.instanceOf(el, HTMLElement, 'Instance of HTMLElement');
      }
      assert.equal(el.title, 'my title', 'Argument passed to constructor');
      assert.isTrue(Polymer.instanceof(el, HTMLElement));
      assert.isTrue(Polymer.instanceof(el, MyElement2));
      assert.isTrue(Polymer.isInstance(el));
    });

    test('imperative dom-module', function() {
      var d = document.createElement('dom-module');
      var c = document.createElement('div');
      d.appendChild(c);
      d.register('my-module');
      var lookup = document.createElement('dom-module');
      assert.equal(lookup.import('my-module', 'div'), c);
    });

    test('mixed-case is (element type) forced to lowercase', function() {
      var a = document.createElement('x-mixed-case');
      assert.equal(a.is, 'x-mixed-case');
      var b = document.createElement('button', 'x-mixed-case-button');
      assert.equal(b.is, 'x-mixed-case-button');
      assert.equal(b.getAttribute('is'), 'x-mixed-case-button');
    });

    test('input to Polymer is a `class`', function() {
      var _class = function() {};
      _class.prototype = {
        is: 'x-class-as-argument-test'
      };
      Polymer(_class);
      var a = document.createElement('x-class-as-argument-test');
      assert.equal(a.is, 'x-class-as-argument-test');
    });

    test('input to Polymer is falsey', function() {
      var a = document.createElement('falsey-polymer-arg');
      assert.equal(a.is, 'falsey-polymer-arg');
    });
  });

</script>
</body>
</html>
